package com.edan.partnerback.controller;

import com.edan.partnerback.common.VO.Result;
import com.edan.partnerback.common.VO.UserRequest;
import com.edan.partnerback.common.annotation.AutoLog;
import com.edan.partnerback.entity.User;
import com.edan.partnerback.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 功能描述
 * @Author Edan
 * @Create 2024/3/28 14:29
 */
@Api(tags = "无权限校验接口")
@RestController
@Slf4j
public class WebController {
    @Autowired
    private IUserService userService;

    @AutoLog("校验版本号")
    @ApiOperation(value = "校验版本号")
    @GetMapping("/")
    public String version() {
        String ver = "partner-back-0.0.1-SNAPSHOT";//默认版本号
        Package Package = WebController.class.getPackage();
        String title = Package.getImplementationTitle();
        String version = Package.getImplementationVersion();
        if (title != null && version != null) {
            ver = String.join("-", title, version);
        }
        return ver;
    }
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        return Result.success(userService.login(user));
    }

    @ApiOperation(value = "用户退出")
    @GetMapping("/logout/{uid}")
    public Result logout(@PathVariable String uid) {
        userService.logout(uid);
        return Result.success();
    }

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public Result register(@RequestBody UserRequest user) {
        userService.register(user);
        return Result.success("注册成功，请重新登录");
    }

    @ApiOperation(value = "邮箱验证")
    @GetMapping("/email")
    public Result sendEmail(@RequestParam String email, @RequestParam String type) {
        userService.sendEmail(email, type);
        return Result.success();
    }

    @ApiOperation(value = "密码重置", tags = "传email 和 emailCode")
    @PostMapping("/password/reset")
    public Result passwordReset(@RequestBody UserRequest userRequest) {
        String newPassword = userService.passwordReset(userRequest);
        return Result.success("新的密码为：" + newPassword);
    }

    @ApiOperation(value = "密码修改", tags = "uid 和 oldPassword 和 newPassword")
    @PostMapping("/password/change")
    public Result passwordUpdate(@RequestBody UserRequest userRequest) {
        String newPassword = userService.passwordUpdate(userRequest);
        return Result.success("新的密码为：" + newPassword);
    }

    @ApiOperation(value = "密码重置前的账号验证", tags = "可以传username 或者 email")
    @GetMapping("/password/check")
    public Result passwordCheck(@RequestBody UserRequest userRequest) {
        String phoneNumber = userService.passwordReset(userRequest);
        return Result.success();
    }




}
